# Contributor: Piper McCorkle <piper@cloudflare.com>
# Maintainer: Piper McCorkle <piper@cloudflare.com>
pkgname=cloudflared
pkgver=2024.10.0
pkgrel=0
pkgdesc="Cloudflare Tunnel client"
url="https://github.com/cloudflare/cloudflared"
# ppc64le/riscv64/loongarch64: Not supported by upstream
arch="all !ppc64le !riscv64 !loongarch64"
license="Apache-2.0"
makedepends="go gettext"
subpackages="$pkgname-doc $pkgname-openrc"
pkgusers="$pkgname"
pkggroups="$pkgname"
install="$pkgname.pre-install"
source="$pkgname-$pkgver.tar.gz::https://github.com/cloudflare/cloudflared/archive/refs/tags/$pkgver.tar.gz
	goflags.patch
	config.yml
	$pkgname.initd
	$pkgname.confd
	"
options="!check" # require privileged icmp sockets

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

prepare() {
	default_prepare
	go mod vendor
}

build() {
	export VERSION="$pkgver" DATE="$(date -u '+%Y-%m-%d-%H%M UTC' -d @"$SOURCE_DATE_EPOCH")"
	make VERSION="$VERSION" DATE="$DATE" cloudflared
	envsubst < cloudflared_man_template > cloudflared.1
}

check() {
	_goarch=$(go tool dist env | grep GOARCH | sed 's/^GOARCH="//; s/"$//')
	# Go race detector is only supported on amd64, ppc64le, and arm64
	if [ $_goarch = "amd64" ] || [ $_goarch = "ppc64le" ] || [ $_goarch = "arm64" ]; then
		_race=-race
	fi
	go test -mod=vendor -buildmode=default $_race ./...
}

package() {
	install -D -m755 ./cloudflared "$pkgdir"/usr/bin/cloudflared
	install -D -m644 ./cloudflared.1 "$pkgdir"/usr/share/man/man1/cloudflared.1

	install -D -m755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -D -m755 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -D -m644 "$srcdir"/config.yml "$pkgdir"/etc/$pkgname/config.yml
}

sha512sums="
751369a310f666869cc008a71f1de4697aefd2f5c57a87894ce60ad65afaa26b8af92bbbb80ee8f85c712e3050d7e8eebc3574179c5ca09de76ddef794dbdb13  cloudflared-2024.10.0.tar.gz
e5071da9eb2b01bfc678366b21a77ffb319de54e41c6a8d02f99831b0c3a6e9117fa3f8680eba5b987dd404d22a257c7903fb14edae37fd1ff18d28f8a2e16e3  goflags.patch
01f43af92c2c804f6b31189b78f81a76f078179065bebf12118a8bb3ba22e1c8042ef760f24acdf1356bbe5235df7d2a00d55a158e5fec5063c5d2d6abcd7dee  config.yml
76b684ef4d0267e8546a21cc60239ef94cbb5fd6b3c0f4fbf4e843eb6e8b56943d3200a160e1749b228306e5649e6a1fa90a58f14fb68c5794f2c760a6c14506  cloudflared.initd
47c71a328d49f3d35dd6e0268e1d734cd21ecc84b24459f77a46df7632f9a8fdb7817cf49187f965c13c94017e4454897caa99ca12b41037dfe0455bb156ef09  cloudflared.confd
"
